package com.android.place360.database;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class Catalog_DAO {
	
	public static boolean AddCatalog(Context context, Catalog catalog) {
		boolean result = true;
		DBAdapter mDB = new DBAdapter(context);
		try {
			
			mDB.open();
			mDB.addCatalog(catalog.getName(), catalog.getSearch_key(), catalog.getSup_Catalog(), catalog.getIcon());
		} catch (Exception ex) {
			Log.i("error", "error " + ex.getMessage());
			result = false;
		}
		finally{
			mDB.close();
		}
		return result;
	}
	
	public static boolean UpdateCatalog(Context context, Catalog catalog) {
		boolean result = true;
		DBAdapter mDB = new DBAdapter(context);
		try {
			
			mDB.open();
			mDB.updateCatalog(catalog.getId(),catalog.getName(), catalog.getSearch_key(), catalog.getSup_Catalog(), catalog.getIcon());
		} catch (Exception ex) {
			Log.i("error", "error " + ex.getMessage());
			result = false;
		}
		finally{
			mDB.close();
		}
		return result;
	}

	public static ArrayList<Catalog> getAllCatalog(Context context) {
		ArrayList<Catalog> catalogs = new ArrayList<Catalog>();
		DBAdapter mDB= new DBAdapter(context);
		try{
			 
		mDB.open();
		Cursor mCursor = mDB.getAllCatalog();
		/* retrieve data from database  */

		int nId = mCursor.getColumnIndex(DBAdapter.C_ID);
		int nName = mCursor.getColumnIndex(DBAdapter.C_NAME);
		int nSearch_key = mCursor.getColumnIndex(DBAdapter.C_SEARCH_KEY);
		int nIcon = mCursor.getColumnIndex(DBAdapter.C_ICON);
		
		
		// Check if our result was valid.
		mCursor.moveToFirst();
		if (mCursor != null) {
			// Loop through all Results
			do {
				Catalog ct = new Catalog();
				ct.setId(mCursor.getInt(nId));
				ct.setName(mCursor.getString(nName));
				ct.setSearch_key(mCursor.getString(nSearch_key));
				ct.setIcon(mCursor.getInt(nIcon));
				catalogs.add(ct);
			} while (mCursor.moveToNext());
			
			mCursor.close();
		}
		}catch (Exception e) {
			Log.i("error", "error " + e.getMessage());
		}
		finally{
			mDB.close();
		}
		return catalogs;
	}
	
	public static Catalog getCatalog(Context context, int id) {
		Catalog ct = new Catalog();
		DBAdapter mDB= new DBAdapter(context);
		try{
			 
		mDB.open();
		Cursor mCursor = mDB.getCatalogByID(id);
		/* retrieve data from database  */

		int nId = mCursor.getColumnIndex(DBAdapter.C_ID);
		int nName = mCursor.getColumnIndex(DBAdapter.C_NAME);
		int nSearch_key = mCursor.getColumnIndex(DBAdapter.C_SEARCH_KEY);
		int nIcon = mCursor.getColumnIndex(DBAdapter.C_ICON);
		
		
		// Check if our result was valid.
		mCursor.moveToFirst();
		if (mCursor != null) {

				ct.setId(mCursor.getInt(nId));
				ct.setName(mCursor.getString(nName));
				ct.setSearch_key(mCursor.getString(nSearch_key));
				ct.setIcon(mCursor.getInt(nIcon));
				
				mCursor.close();
		}
		}catch (Exception e) {
			Log.i("error", "error " + e.getMessage());
		}
		finally{
			mDB.close();
		}
		return ct;
	}
	
	public static boolean deleteCatalog(Context context, int id){
		boolean result = true;
		DBAdapter mDB = new DBAdapter(context);
		try {
			
			mDB.open();
			mDB.deleteCatalog(id);
		} catch (Exception ex) {
			Log.i("error", "error " + ex.getMessage());
			result = false;
		}
		finally{
			mDB.close();
		}
		return result;
	}
	
	public static boolean deleteAllCatalog(Context context) {
		boolean result = true;
		DBAdapter mDB = new DBAdapter(context);
		try {
			
			mDB.open();
			mDB.deleteAllCatalog();
		} catch (Exception ex) {
			Log.i("error", "error " + ex.getMessage());
			result = false;
		}
		finally{
			mDB.close();
		}
		return result;
	}
}
